import uvicorn

from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import JSONResponse, Response
from starlette.staticfiles import StaticFiles

from apps.utils.sql_conn import lifespan

from fastapi.responses import FileResponse

from apps.auth import auth
from apps.medicine import medic
from apps.doctor import doctor
from apps.interface import interface
from apps.fabric import fabric

from apps.neo4j import neo4j

app = FastAPI(default_response_class=JSONResponse, lifespan=lifespan)

# 配置 CORS 中间件
origins = [
    "*",
]


class StaticFilesWithCORS(StaticFiles):
    async def get_response(self, path: str, scope) -> Response:
        response = await super().get_response(path, scope)
        response.headers["Access-Control-Allow-Origin"] = "*"
        return response


app.mount("/static", StaticFilesWithCORS(directory="static"), name="static")

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有 HTTP 方法
    allow_headers=["*"],  # 允许所有头部
)

# 包含其他路由
app.include_router(auth, tags=["01 用户验证接口"])
app.include_router(medic, tags=["02 药品处理接口"])
app.include_router(doctor, tags=["03 医生接口"])
app.include_router(interface, tags=["04 页面接口"])
app.include_router(neo4j, tags=["05 neo4j问答模型"])
app.include_router(fabric, tags=["06 区块链接口"])



@app.get('/favicon.ico', include_in_schema=False)
async def favicon():
    return FileResponse('favicon.ico')


if __name__ == '__main__':
    print('接口文档链接：http://127.0.0.1:8080/docs')
    uvicorn.run("main:app", port=8080, reload=True)
